package gm

import (
	"fmt"
	"go_server/game_server/api/service/bag"
	"go_server/game_server/api/service/build"
	"go_server/game_server/api/service/buildrobot_info"
	"go_server/game_server/api/service/hero_info"
	"go_server/game_server/api/service/main_task"
	"go_server/game_server/constants"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/zinx/ziface"
	"go_server/zinx/znet"
	"strconv"
)

const (
	unlock_map_block    = 1 //解锁地块
	add_item            = 2 //添加物品
	sub_item            = 3 //扣除物品
	unlock_all_block    = 4 //一键解锁所有地块建筑
	upgrade_build_level = 5 //建筑升级
	gm_build_click_type = 6 //建筑交互操作

	send_all_robot_AI                 = 2000 //请求获得所有机器人信息
	add_robot_2_build_lv_up           = 2001 //建筑升级添加机器人[robotserid, mapid, buildcfgid, buildserid]
	del_robot_2_build_lv_up           = 2002 //建筑升级删除机器人[robotserid, mapid, buildcfgid, buildserid]
	checklvupover_robot_2_build_lv_up = 2003 //检测是否有升级完成的建筑
	delalllvup_robot_2_build_lv_up    = 2004 //删除所有建造中的机器人
	buildrobot_chargebyitem           = 2006 //使用道具给机器人充电

	useritem_upgrade_robot_2_build_lv_up = 2100
	setuplvtotaltime_robot_2_build_lv_up = 2101 //[2007|mapid, buildcfgid, buildserid,totaltime]指定建造中的建筑升级的总共升级时间(秒)
	appointsucover_robot_2_build_lv_up   = 2102 //指定建造中的建筑升级成功[mapid, buildcfgid, buildserid]
	sucallover_robot_2_build_lv_up       = 2103 //所有建造中的建筑升级成功

	gm_GetHeros          = 4000 //[]请求当前拥有的所有英雄
	gm_AddHero           = 4001 //[]添加英雄
	gm_DelHero           = 4002 //[]删除英雄--功能不做--保留协议号
	gm_UpLvHero          = 4003 //[]英雄升级
	gm_UpStarHero        = 4004 //[]英雄升星
	gm_UnlockHero        = 4005 //[]解锁英雄
	gm_UniToExcFragments = 4006 //[]通用碎片转专属碎片

	gm_GetCells     = 4100 //[]当前拥有的所有细胞
	gm_AddCell      = 4201 //[]添加细胞
	gm_FuseCell     = 4202 //[]融合细胞
	gm_RecombinCell = 4203 //[]重组细胞
	gm_EquipCell    = 4204 //[]装备细胞
	gm_UnEquipCell  = 4205 //[]卸下细胞

	gm_AddCfgIdHero      = 4900 //[HeroCfgId]添加指定配置Id的英雄
	gm_AddDefHeros       = 4901 //[]添加默认解锁的英雄
	gm_AddAllCells       = 4902 //[]添加指定数量的所有细胞
	gm_OneKeyFuseAllCell = 4903 //[]一键融合所有细胞

	gm_MainTask = 6000 // 任务进度推送
)

type gmCommandApi struct {
	znet.BaseRouter
}

func (*gmCommandApi) Handle(request ziface.IRequest) {
	msg := &pb.GMCommandC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	gmType := msg.Type
	params := msg.Params
	roleId := serviceObj.GetRoleId(request)

	conn := request.GetConnection()
	code := gmHandle(conn, roleId, gmType, params)

	res := new(pb.GMCommandS2C)
	res.RetCode = code
	res.Type = gmType
	res.Params = params

	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GMCommand), res)
}

func gmHandle(conn ziface.IConnection, roleId int64, gmType int32, params []string) pb.ErrorCodeManager {
	switch gmType {
	case unlock_map_block:
		{
			mapId, err := strconv.Atoi(params[0])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			return build.UnlockRoleMapBlock(conn, roleId, int32(mapId), 1, false)
		}
	case send_all_robot_AI:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_GetAllBuildRobots(conn, roleId, params)
		}
	case add_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_AddBuildRobotLvUp(conn, roleId, params)
		}
	case del_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_DelBuildRobotLvUp(conn, roleId, params)
		}
	case checklvupover_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_CheckBuildRobotLvUpOver(conn, roleId)
		}
	case delalllvup_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_DelAllBuildRobotLvUp(conn, roleId, params)
		}
	case buildrobot_chargebyitem:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_BuildRobotChargeByItem(conn, roleId, params)
		}
	case setuplvtotaltime_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_SetBuildRobotLvUpTime(conn, roleId, params)
		}
	case appointsucover_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_BuildRobotLvUpOverSuc(conn, roleId, params)
		}
	case sucallover_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_AllBuildRobotLvUpOverSuc(conn, roleId, params)
		}
	case useritem_upgrade_robot_2_build_lv_up:
		{
			return buildrobot_info.Buildrobot_info_serviceObj.GM_BuildRobotUpgradeByItem(conn, roleId, params)
		}
	case add_item:
		{
			if len(params) <= 0 || len(params)%2 != 0 {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			itemMap := make(map[int32]int32)
			for i := 0; i < len(params); i += 2 {
				itemId, err := strconv.Atoi(params[i])
				if err != nil {
					return pb.ErrorCodeManager_PARAM_ERROR
				}

				itemCount, err := strconv.Atoi(params[i+1])
				if err != nil {
					return pb.ErrorCodeManager_PARAM_ERROR
				}

				itemMap[int32(itemId)] = int32(itemCount)
			}
			bag.AddItemBatch(roleId, itemMap)
		}
	case sub_item:
		{
			if len(params) <= 0 || len(params)%2 != 0 {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			itemMap := make(map[int32]int32)
			for i := 0; i < len(params); i += 2 {
				itemId, err := strconv.Atoi(params[i])
				if err != nil {
					return pb.ErrorCodeManager_PARAM_ERROR
				}

				itemCount, err := strconv.Atoi(params[i+1])
				if err != nil {
					return pb.ErrorCodeManager_PARAM_ERROR
				}

				itemMap[int32(itemId)] = int32(itemCount)
			}
			retCode, _ := bag.SubItemBatch(roleId, itemMap)
			return retCode
		}
	case unlock_all_block:
		{
			mapId, err := strconv.Atoi(params[0])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			return build.UnlockRoleAllBlock(conn, roleId, int32(mapId))
		}
	case upgrade_build_level:
		{
			if len(params) <= 0 || len(params) != 3 {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			_, _, ok := build.UpdateBuildLevel(fmt.Sprint(roleId) + "-" + params[0] + "-" + params[1] + "-" + params[2])
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
		}
	case gm_build_click_type:
		{
			if len(params) <= 0 || len(params) != 4 {
				return pb.ErrorCodeManager_PARAM_ERROR
			}

			mapId, err := strconv.Atoi(params[0])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}

			cfgBuildId, err := strconv.Atoi(params[1])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			srvBuildId, err := strconv.Atoi(params[2])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			clickType, err := strconv.Atoi(params[3])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}
			code, ok := build.SetBuildClickType(roleId, int32(mapId), int32(cfgBuildId), int32(srvBuildId),
				int32(clickType), conn)
			if !ok {
				return code
			}
		}
	case gm_GetHeros:
		{
			return hero_info.Hero_info_serviceObj.GM_GetHeros(conn, roleId, params)
		} //4000 //[]请求当前拥有的所有英雄
	case gm_AddHero:
		{
			return hero_info.Hero_info_serviceObj.GM_AddHero(conn, roleId, params)
		} //4001 //[]添加英雄
	case gm_DelHero:
		{
			return hero_info.Hero_info_serviceObj.GM_DelHero(conn, roleId, params)
		} //4002 //[]删除英雄--功能不做--保留协议号
	case gm_UpLvHero:
		{
			return hero_info.Hero_info_serviceObj.GM_UpLvHero(conn, roleId, params)
		} //4003 //[]英雄升级
	case gm_UpStarHero:
		{
			return hero_info.Hero_info_serviceObj.GM_UpStarHero(conn, roleId, params)
		} //4004 //[]英雄升星
	case gm_UnlockHero: // = 4005 //[]解锁英雄
		{
			return hero_info.Hero_info_serviceObj.GM_UnlockHero(conn, roleId, params)
		}
	case gm_UniToExcFragments: //= 4006 //[]通用碎片转专属碎片
		{
			return hero_info.Hero_info_serviceObj.GM_UniToExcFragments(conn, roleId, params)
		}
	case gm_AddCfgIdHero:
		{
			return hero_info.Hero_info_serviceObj.GM_AddCfgIdHero(conn, roleId, params)
		} //[HeroCfgId]添加指定配置Id的英雄
	case gm_AddDefHeros:
		{
			return hero_info.Hero_info_serviceObj.GM_AddDefHeros(conn, roleId, params)
		} //4009 //[]添加默认解锁的英雄
	case gm_GetCells: //4100 //[]当前拥有的所有细胞
		{
			return hero_info.Hero_info_serviceObj.GM_GetCells(conn, roleId, params)
		}
	case gm_AddCell: //4101 //[]添加细胞
		{
			return hero_info.Hero_info_serviceObj.GM_AddCell(conn, roleId, params)
		}
	case gm_FuseCell: //4102 //[]融合细胞
		{
			return hero_info.Hero_info_serviceObj.GM_FuseCell(conn, roleId, params)
		}
	case gm_RecombinCell: //4103 //[]重组细胞
		{
			return hero_info.Hero_info_serviceObj.GM_RecombinCell(conn, roleId, params)
		}
	case gm_EquipCell: // 4104 //[]装备细胞
		{
			return hero_info.Hero_info_serviceObj.GM_EquipCell(conn, roleId, params)
		}
	case gm_UnEquipCell: // 4105 //[]卸下细胞
		{
			return hero_info.Hero_info_serviceObj.GM_UnEquipCell(conn, roleId, params)
		}
	case gm_AddAllCells: //4902 //[]添加指定数量的所有细胞
		{
			return hero_info.Hero_info_serviceObj.GM_AddAllCell(conn, roleId, params)
		}
	case gm_OneKeyFuseAllCell: // 4903 //[]一键融合所有细胞
		{
			return hero_info.Hero_info_serviceObj.GM_OneKeyFuseAllCell(conn, roleId, params)
		}
	case gm_MainTask:
		{
			var taskType int32 = constants.MAIN_TASK // 任务类别-主线任务

			taskId, err := strconv.Atoi(params[0])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}

			value, err := strconv.Atoi(params[1])
			if err != nil {
				return pb.ErrorCodeManager_PARAM_ERROR
			}

			taskConfig, ok := config_manager.GetStcTableTask_SheetTaskById(int32(taskId))
			if !ok {
				return pb.ErrorCodeManager_MAIN_TASK_UNFIND
			}

			var id int32 = 0

			if len(taskConfig.Value) > 1 {
				id = taskConfig.Value[0]
			}

			return main_task.ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskConfig.Condition, map[string]int32{
				"id":    id,
				"value": int32(value),
			})
		}
	default:
		return pb.ErrorCodeManager_GM_NOT_EXIST
	}

	return pb.ErrorCodeManager_SUCCESS
}
